package de.haw.algorithm;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class ReadingThread extends Thread {

	private boolean atEnd = false;

	private final DatenContainer<String> container = new DatenContainer<>();

	private final BufferedReader br;

	public ReadingThread(File file) throws FileNotFoundException {
		FileInputStream fstream = new FileInputStream(file);
		DataInputStream in = new DataInputStream(fstream);
		br = new BufferedReader(new InputStreamReader(in));
	}

	public String getElemt() throws InterruptedException {
		return container.raus();
	}

	public boolean isAtEnd() {
		return atEnd && container.size() < 1;
	}

	@Override
	public void run() {
		super.run();
		String strLine;
		try {
			while ((strLine = br.readLine()) != null) {
				container.rein(strLine);
			}
			atEnd = true;
		} catch (IOException | InterruptedException e) {
			e.printStackTrace();
		} finally {
			interrupt();
		}
	}

}
